package com.meiyu.live.ui.main;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.meiyu.live.BaseActivity;
import com.meiyu.live.R;

/**
 * Created by zbl on 2017/1/6.
 */
public class GuidanceActivity extends BaseActivity {

    private static final int[] IMAGE_RES = {
            R.drawable.guide_page0,
            R.drawable.guide_page1,
            R.drawable.guide_page2,
            R.color.trans
    };

    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_guidance);
        init();
    }

    private void init() {
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mViewPager.setAdapter(new GuidanceAdapter());
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (state == ViewPager.SCROLL_STATE_IDLE) {
                    if (mViewPager.getCurrentItem() == IMAGE_RES.length - 1) {
                        finish();
                    }
                }
            }
        });
    }

    class GuidanceAdapter extends PagerAdapter {

        private ImageView[] imageViews = new ImageView[IMAGE_RES.length];

        GuidanceAdapter() {
            for (int i = 0; i < IMAGE_RES.length; i++) {
                ImageView imageView = new ImageView(GuidanceActivity.this);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                imageView.setImageResource(IMAGE_RES[i]);
                imageViews[i] = imageView;
            }
        }

        @Override
        public int getCount() {
            return IMAGE_RES.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(imageViews[position]);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewPager.LayoutParams.MATCH_PARENT);
            container.addView(imageViews[position], params);
            return imageViews[position];
        }
    }
}
